package com.xqb.common.utils;

import java.util.Random;

/**
 * @Author: JinChen Zhu
 * @CreateTime: 2024-11-02  12:00
 */
public class CommonUtils {
    /**
     * 生成指定长度的随机验证码
     *
     * @param length 验证码长度
     * @return 随机验证码
     */
    public static String generateCode(int length) {
        // 确保验证码长度不超过9
        if (length > 9) {
            length = 9;
        }
        // 随机数生成器
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < length; i++) {
            // 生成0-9的随机数字
            code.append(random.nextInt(10));
        }
        return code.toString();
    }

    public static boolean isMobile(String phone) {
        return phone.matches("^1[3-9]\\d{9}$"); // 正则表达式匹配手机号码
    }
}
